package com.ruoyi.web.service.impl;

import com.ruoyi.common.base.user.domain.Agent;
import com.ruoyi.common.base.user.mapper.AgentMapper;
import com.ruoyi.common.base.user.mapstruct.AgentStruct;
import com.ruoyi.common.base.user.vo.AgentAppVO;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.customize.util.CommonUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.web.service.IAgentService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 代理信息Service业务层处理
 *
 * @author gdk
 * @date 2023-05-27
 */
@Slf4j
@Service
@AllArgsConstructor
public class AgentServiceImpl implements IAgentService {
    private final AgentMapper agentMapper;

    /**
     * 查询代理信息
     *
     * @param id 代理信息主键
     * @return 代理信息
     */
    @Override
    public Agent selectAgentById(Long id) {
        return agentMapper.selectAgentById(id);
    }

    /**
     * 查询代理信息列表
     *
     * @param agent 代理信息
     * @return 代理信息
     */
    @Override
    public List<Agent> selectAgentList(Agent agent) {
        return agentMapper.selectAgentList(agent);
    }

    /**
     * 新增代理信息
     *
     * @param agent 代理信息
     * @return 结果
     */
    @Override
    public int insertAgent(Agent agent) {
        agent.setCreateTime(DateUtils.getNowDate());
        return agentMapper.insertAgent(agent);
    }

    /**
     * 修改代理信息
     *
     * @param agent 代理信息
     * @return 结果
     */
    @Override
    public int updateAgent(Agent agent) {
        agent.setUpdateTime(DateUtils.getNowDate());
        return agentMapper.updateAgent(agent);
    }

    /**
     * 批量删除代理信息
     *
     * @param ids 需要删除的代理信息主键
     * @return 结果
     */
    @Override
    public int deleteAgentByIds(Long[] ids) {
        return agentMapper.deleteAgentByIds(ids);
    }

    /**
     * 删除代理信息信息
     *
     * @param id 代理信息主键
     * @return 结果
     */
    @Override
    public int deleteAgentById(Long id) {
        return agentMapper.deleteAgentById(id);
    }

    /**
     * 获取当前用户的代理信息
     *
     * @return {@link AgentAppVO}
     * @author gdk
     * @since 2023/5/27 14:14
     */
    @Override
    public AgentAppVO current() {
        LoginUser loginUser = CommonUtil.getLoginUserNotNull();
        Agent condition = new Agent();
        condition.setId(loginUser.getUserId());
        Agent agent = agentMapper.getOne(condition);

        return AgentStruct.INSTANCE.entity2AppVO(agent);
    }
}
